(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('PubCardCommentsController', PubCardCommentsController);
    
    angular
    .module('milkbeeApp').filter('trustHtml', ['$sce',function ($sce) {
        return function (input) {
            return $sce.trustAsHtml(input);
        }
    }]);
    PubCardCommentsController.$inject = ['$scope', '$state','$http', 'PubCardComments', 'Card','ParseLinks', 'AlertService', 'paginationConstants', 'pagingParams'];

    function PubCardCommentsController ($scope, $state,$http, PubCardComments,Card, ParseLinks, AlertService, paginationConstants, pagingParams) {
        var vm = this;

        vm.loadPage = loadPage;
        vm.predicate = pagingParams.predicate;
        vm.reverse = pagingParams.ascending;
        vm.transition = transition;
        vm.itemsPerPage = paginationConstants.itemsPerPage;
        vm.categoryName=pagingParams.categoryName;
        vm.categoryId=pagingParams.categoryId;
        vm.light=light;
        vm.accusation=accusation;
        vm.moderators=[];
        loadAll();
        vm.focus=function(obj){
        	$http({
    			method: 'PUT',
    			url: '/api/user/focus/'+obj.id,
    			params:{'id':obj.id}
    		}).then(function successCallback(response) {
    			 AlertService.success("success");
    		}, function errorCallback(response) {
    			AlertService.error("fail");
    		});
        }
        function loadAll () {
        	PubCardComments.query({
        		cardId:pagingParams.cardId,
        		cardName:pagingParams.cardName,
                page: pagingParams.page - 1,
                size: vm.itemsPerPage,
                sort: sort()
            }, onSuccess, onError);
            function sort() {
                var result = [vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc')];
                if (vm.predicate !== 'id') {
                    result.push('id');
                }
                return result;
            }
            function onSuccess(data, headers) {
                vm.links = ParseLinks.parse(headers('link'));
                vm.totalItems = headers('X-Total-Count');
                vm.queryCount = vm.totalItems;
                vm.comments = data;
                vm.page = pagingParams.page;
                vm.cardName=pagingParams.cardName;
                vm.cardId=pagingParams.cardId;
                
            }
            function onError(error) {
                AlertService.error(error.data.message);
            }
        }

        function loadPage(page) {
            vm.page = page;
            vm.transition();
        }

        function transition() {
            $state.transitionTo($state.$current, {
                page: vm.page,
                sort: vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc'),
                search: vm.currentSearch
            });
        }
        function light(obj){
        	if(obj.main){
        		$http({
        			method: 'GET',
        			url: '/api/pub/card/light/'+obj.id,
        			params:{'id':obj.id}
        		}).then(function successCallback(response) {
        				
        				
        			}, function errorCallback(response) {
        				
        				console.log('fail');
        		});
        	}else{
        		$http({
        			method: 'GET',
        			url: '/api/comment/light/'+obj.id,
        			params:{'id':obj.id}
        		}).then(function successCallback(response) {
        				
        				
        			}, function errorCallback(response) {
        				
        				console.log('fail');
        		});
        	}
        }
        function accusation(obj){
        	if(obj.main){
        		$http({
        			method: 'GET',
        			url: '/api/pub/card/accusation/'+obj.id,
        			params:{'id':obj.id}
        		}).then(function successCallback(response) {
        			
        		}, function errorCallback(response) {
        			console.log('fail');
        		});
        	}else{
        		$http({
        			method: 'GET',
        			url: '/api/comment/accusation/'+obj.id,
        			params:{'id':obj.id}
        		}).then(function successCallback(response) {
        			// AlertService.error(response.data);
        		}, function errorCallback(response) {
        			// AlertService.error(response.data);
        		});
        	}
        }
        
        function loadRanks(){
        	$http({
    			method: 'GET',
    			url: '/api/estates'
    		}).then(function successCallback(response) {
    			vm.estates=response.data;
    		}, function errorCallback(response) {
    			// AlertService.error(response.data);
    		});
        }
        
        function loadModerators(cardId){
        	$http({
    			method: 'GET',
    			url: '/api/allmoderator/'+cardId
    		}).then(function successCallback(response) {
    			var moder=response.data;
    			for(var i=0;i<moder.length;++i){
    				vm.moderators[moder[i].userId]=true;
    			}
    			
    		}, function errorCallback(response) {
    			// AlertService.error(response.data);
    		});
        }
        vm.favorite=function(obj){
        	$http({
    			method: 'POST',
    			url: '/api/my/favorite/card/'+obj.id
    		}).then(function successCallback(response) {
    			//var moder=response.data;
    			AlertService.error('success');
    		}, function errorCallback(response) {
    			// AlertService.error(response.data);
    			AlertService.error('fail');
    		});
        }
        loadRanks();
        loadModerators(pagingParams.cardId);
    }
})();
